<script setup>
import api from '../api/categories_api.js'
import {ref, onMounted} from 'vue'
import {ElMessageBox, ElMessage} from 'element-plus'
import lodash from 'lodash'

// 表格数据
const tableData = ref([])
const total = ref(0)

// 查询参数
const searchObj = ref({
  current: 1,
  limit: 10,
  categoryName: '',
  parentId: ''
})

// 编辑/新增对象
const obj = ref({})
const editWinVisible = ref(false)

// 页面加载后挂载数据
onMounted(async () => {
  await search()
})

// 查询方法
const search = async () => {
  try {
    const res = await api.page(searchObj.value)
    tableData.value = res.data || []
    total.value = res.count || 0
  } catch (error) {
    console.error(error)
    tableData.value = []
    total.value = 0
  }
}

// 新增分类对象
const newCategory = ref({})
const addWinVisible = ref(false)

const showAddDialog = () => {
  newCategory.value = {}
  addWinVisible.value = true
}

const addCancel = () => {
  newCategory.value = {}
  addWinVisible.value = false
}

// 提交新增
const addCommit = async () => {
  try {
    await api.add({ ...newCategory.value })
    ElMessage({ message: '新增成功', type: 'success' })
    await search()
    addWinVisible.value = false
  } catch (error) {
    console.error(error)
    ElMessage({ message: '新增失败', type: 'error' })
  }
}

// 分页事件
const handleSizeChange = (val) => {
  searchObj.value.limit = val
  search()
}

const handleCurrentChange = (val) => {
  searchObj.value.current = val
  search()
}

// 编辑取消
const editCancel = () => {
  obj.value = {}
  editWinVisible.value = false
}

// 提交编辑
const editCommit = async () => {
  try {
    await api.edit(obj.value)
    editWinVisible.value = false
    await search()
    ElMessage({message: '修改成功', type: 'success'})
  } catch (error) {
    console.error(error)
    ElMessage({message: '修改失败', type: 'error'})
  }
}

// 编辑按钮点击
const handleEdit = (index, row) => {
  obj.value = lodash.cloneDeep(row)
  editWinVisible.value = true
}

// 删除按钮点击
const handleDelete = (index, row) => {
  ElMessageBox.confirm('确认删除该分类吗?', '提示', {
    confirmButtonText: '删除',
    cancelButtonText: '取消',
    type: 'warning'
  })
      .then(async () => {
        await api.del(row.categoryId)
        searchObj.value.current = 1
        await search()
        ElMessage({message: '删除成功', type: 'success'})
      })
      .catch(() => {})
}
</script>

<template>
  <div style="padding: 20px;">
    <h2>分类列表</h2>

    <!-- 查询框 -->
    <el-form :inline="true" :model="searchObj" style="margin-bottom: 10px;">
      <el-form-item label="分类名称">
        <el-input v-model="searchObj.categoryName" placeholder="请输入分类名称" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="showAddDialog">新增分类</el-button>
      </el-form-item>
    </el-form>

    <!-- 新增分类对话框 -->
    <el-dialog v-model="addWinVisible" title="新增分类" width="400">
      <el-form :model="newCategory" label-width="100px">
        <el-form-item label="分类名称">
          <el-input v-model="newCategory.categoryName"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="addCancel">取消</el-button>
          <el-button type="primary" @click="addCommit">提交</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 分类表格 -->
    <el-table :data="tableData" border style="width: 100%;">
      <el-table-column prop="categoryId" label="分类ID" width="80"/>
      <el-table-column prop="categoryName" label="分类名称" width="150"/>
      <el-table-column prop="createTime" label="创建时间" width="180"/>
      <el-table-column label="操作" fixed="right" width="150">
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
        v-model:current-page="searchObj.current"
        v-model:page-size="searchObj.limit"
        :total="total"
        :page-sizes="[10,20,30,40]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />

    <!-- 编辑分类对话框 -->
    <el-dialog v-model="editWinVisible" title="编辑分类" width="400">
      <el-form :model="obj" label-width="100px">
        <el-form-item label="分类名称">
          <el-input v-model="obj.categoryName"/>
        </el-form-item>
<!--        <el-form-item label="父分类ID">-->
<!--          <el-input v-model="obj.parentId"/>-->
<!--        </el-form-item>-->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="editCancel">取消</el-button>
          <el-button type="primary" @click="editCommit">提交</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
h2 {
  color: rgba(244, 64, 64, 0.35);
  margin-bottom: 20px;
}
</style>
